package org.fle4y.app.server.database;

import java.sql.SQLException;
import java.util.Vector;


/**
 * author : xiehui<br>
 * company : northking <br>
 * Create at: 2012-6-25<br>
 * 
 * @version : 1.0<br>
 *          mailto:flexie@foxmail.com<br>
 *          Description : 表
 */
public class Table {
	private String name;
	private Vector<Column> columns;
	private int primaryKey;
	private Database db;
	private int columnNum;
	private int visibleColumnNum;
	private Vector<Index> indexes;
	private int indexNum;
	private Vector<Constraint> constraintes;
	private int constraintNum;
	public Cache	cCache;
	public Table() {
	}

	public Table(Database db, String name) {
		this.db = db;
		this.name = name;
		primaryKey = -1;
		columns = new Vector<Column>();
		indexes = new Vector<Index>();

	}

	public void addColumn(String name, int type, boolean nullable)
			throws SQLException {
		this.columns.add(new Column(name, type, nullable));
		this.columnNum++;
	}

	public void createPrimaryKey(int column) throws SQLException {

		visibleColumnNum = columnNum;
		primaryKey = column;
		int col[] = { column };
		createIndex(col, "SYSTEM_PK", true);

	}

	public void createPrimaryKey() throws SQLException {
		this.addColumn("SYSTEM_ID", Column.INTEGER, true);
		createPrimaryKey(columnNum - 1);
		visibleColumnNum = columnNum - 1;
	}

	public void createIndex(int column[], String name, boolean unique)
			throws SQLException {
		for (int i = 0; i < indexNum; i++) {
			Index index = getIndex(i);
			if (name.equals(index.getName())) {
				throw new SQLException("重复的索引 !");
			}
		}
		int s = column.length;
		int col[] = new int[unique ? s : s + 1];
		int type[] = new int[unique ? s : s + 1];
		for (int j = 0; j < s; j++) {
			col[j] = column[j];
			type[j] = getColumn(col[j]).type;
		}

		if (!unique) {
			col[s] = primaryKey;
			type[s] = getColumn(primaryKey).type;
		}

		Index index = new Index(name, col, type, unique);
		indexes.add(index);
		indexNum++;

	}

	public Column getColumn(int i) {
		return columns.elementAt(i);
	}
    int getInternalColumnCount() {
    	return columnNum;
    }

	public Index getIndexForColumns(int col[]) throws SQLException {
		for (int i = 0; i < indexNum; i++) {
			Index h = getIndex(i);
			int icol[] = h.getColumns();
			int j = 0;
			for (; j < col.length; j++) {
				if (j >= icol.length) {
					break;
				}
				if (icol[j] != col[j]) {
					break;
				}
			}
			if (j == col.length) {
				return h;
			}
		}
		return null;
	}

	public Index getIndex(int i) {
		return indexes.elementAt(i);
	}

	public int getColumnNr(String c) throws SQLException {
		int i = searchColumn(c);
		if (i == -1) {
			throw new SQLException("未能找到字段 ！");
		}
		return i;
	}

	public int searchColumn(String c) {
		for (int i = 0; i < columnNum; i++) {
			if (c.equals(((Column) columns.elementAt(i)).name)) {
				return i;
			}
		}
		return -1;
	}

	public Object[] getNewRow() {
		return new Object[columnNum];
	}

	public boolean isEmpty() {
		return getIndex(0).getRoot() == null;
	}

	public String getName() {
		return name;
	}

	public void addConstraint(Constraint c) {
		constraintes.addElement(c);
		constraintNum++;
	}
    int getIndexCount() {
    	return indexNum;
        }
    
    int getType(int i) {
    	return getColumn(i).type;
        }

}
